include $(MPP_SRC_DIR)/userapps/sample/mpp.mk
CC=riscv64-unknown-linux-musl-gcc
CC_CFLAGS=-mcmodel=medany -march=rv64imafdcv -mabi=lp64d -Wall -O0 -g -gdwarf-2 -n --static $(KCFLAGS)
CPP=riscv64-unknown-linux-musl-g++
LINKFLAG=-T $(MPP_SRC_DIR)/userapps/sample/linker_scripts/riscv64/link.lds -lpthread

CURRECT_DIR_NAME=$(shell basename `pwd`)
LOCAL_SRC_DIR = $(shell pwd)
DIRS = $(shell find $(LOCAL_SRC_DIR) -maxdepth 3 -type d) 
BIN = $(MPP_SRC_DIR)/userapps/sample/elf/$(CURRECT_DIR_NAME).elf
LIBPATH = $(MPP_LIB_PATH)
LIBS = $(MPP_LIBS)
SUBDIR += ./zbar
SUBDIR += ./zbar/decoder
SUBDIR += ./zbar/qrcode

LOCAL_CFLAGS = -I$(LOCAL_SRC_DIR)\
			   -I$(LOCAL_SRC_DIR)/../sample_vo
# LOCAL_LDFLAGS = -lX11
INCS := $(foreach dir,$(SUBDIR),-I$(dir))
SRCS =  $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c))

OBJS = $(patsubst %.c,%.o,$(SRCS))

all: $(BIN)
	@-rm -f $(OBJS)
	@echo "${PWD}/Makefile all"

$(OBJS): %.o : %.c
	@echo CC $@
	@$(CC) $(CC_CFLAGS) $(LOCAL_CFLAGS) $(BSP_CFLGAS) $(RTSMART_CFLAGS) $(MPP_USER_CFLGAS)  $(INCS) -c $< -o $@

$(BIN): $(OBJS)
	@echo LD $@
	@$(CC) -o $(BIN) $(LINKFLAG) -Wl,--whole-archive -Wl,--no-whole-archive -n --static $(OBJS) -L$(LIBPATH) -Wl,--start-group $(LIBS) -Wl,--end-group

clean:
	echo "${PWD}/Makefile clean"
	-rm -rf $(BIN)
	-rm -f $(OBJS)

.PHONY: all clean